package com.cblz.bd.server.core.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.cblz.bd.common.core.entity.BaAdmin;
import com.cblz.bd.common.core.utils.ResultResponse;
import com.cblz.bd.server.core.annotation.AdminApiController;
import com.cblz.bd.server.core.entity.dto.BaAdminDTO;
import com.cblz.bd.server.core.service.BaAdminService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * {@code @Name:} AdminUserController
 * {@code @Description: } 后台管理员个人资料控制层
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2022/11/16 - 周三
 */
@Slf4j
@RequiredArgsConstructor
@AdminApiController("routine.AdminInfo")
public class AdminUserController {

    private final BaAdminService baAdminService;
    private final ElasticsearchRestTemplate template;

    @GetMapping("testes")
    public ResultResponse testes() {
        Query query = template.matchAllQuery();
        return new ResultResponse().data(query);
    }

    @GetMapping("index")
    public ResultResponse getInfoById() {
        BaAdmin baAdmin = baAdminService.getById(StpUtil.getLoginId().toString());
        baAdmin.setPassword("");
        return new ResultResponse().data(Map.of("info", baAdmin));
    }

    @PostMapping("upload")
    public ResultResponse uploadAvatar(@RequestParam MultipartFile file) throws Exception {
        return new ResultResponse().data(Map.of("fileUrl", baAdminService.uploadAdminAvatar(file)));
    }

    @PutMapping("edit")
    public ResultResponse edit(@RequestBody BaAdminDTO baAdminDTO) {
        baAdminService.edit(baAdminDTO);
        return new ResultResponse().message("修改管理员头像成功");
    }
}
